import { Alert, Expander, ExpanderItem } from '@aws-amplify/ui-react';
import ReactPropsTable from '@/components/propsTable/ReactPropsTable';
import { Example, ExampleCode } from '@/components/Example';
import { ERROR_MESSAGE } from '../props';
import { CHANGE_PASSWORD, OVERRIDES, PASSWORD_FIELDS, SUBMIT_BUTTON } from './props';
### Props
### Basic Usage
`ChangePassword` has `onSuccess` handler that will be called after successful password change. You may use this callback to run any custom handling, such as navigating route or rendering confirmation messages.
```jsx
import React from 'react';
import { AccountSettings } from '@aws-amplify/ui-react';
function App() {
const handleSuccess = () => {
alert('password is successfully changed!')
}
return (
);
}
```
### Custom Validation
You can override the default password validator with your own custom validator. To do so, you can use the `validators` prop, which takes an array of `ValidatorOptions`:
```ts
interface ValidatorOptions {
validationMode: 'onBlur' | 'onChange' | 'onTouched';
validator: (value: string) => boolean;
message: string;
}
```
- `onBlur` validates password on every blur event
- `onChange` validates password on every change event
- `onTouched` validates password on first blur, and on every change event after the first blur.
For example, following is a minLength validator that validates on every change:
```js
const minLength = {
validationMode: 'onChange',
validator: (password) => password.length >= 4,
message: 'Password must have length 4 or greater',
};
```
You can pass multiple validators to `ChangePassword` to override the default validator:
```jsx
import React from 'react';
import { AccountSettings } from '@aws-amplify/ui-react';
function App() {
const minLength = {
validationMode: 'onChange',
validator: (password) => password.length >= 4,
message: 'Password must have length 4 or greater',
};
const maxLength = {
validationMode: 'onChange',
validator: (password) => password.length <= 12,
message: 'Password must have length 12 or less',
};
const handleSuccess = () => {
alert('password is successfully changed!')
}
return (
);
}
```
### Component Overrides
You can provide your own UI components to override parts of `ChangePassword`. It supports the following slots:
#### Reusing default components
Default components are accessible through `AccountSettings.ChangePassword.COMPONENT_NAME` (e.g. `AccountSettings.ChangePassword.NewPasswordField`) for your convenience. This is useful if you're interested in modifying just a small part of UI instead of overriding the whole component.
```jsx
function App() {
const components = {
NewPasswordField: (props) => (
),
}
return (
);
}
```
Note that spreading props is necessary so that event listeners like `onChange` or html attributes like `name` are passed correctly. If you're providing your own custom components, make sure required props are passed onto your elements.
#### Props and Examples
Here are the required props and examples for overriding each slot.
`ChangePassword` has three password fields that you can override: `CurrentPasswordField`, `NewPasswordField`, and `ConfirmPasswordField`. They all take the same props:
You can reuse the `AccountSettings.ChangePassword.`:
```jsx
function App() {
const components = {
NewPasswordField: (props) => (
),
ConfirmPasswordField: (props) => (
),
}
return (
);
}
```
The following example overrides the `CurrentPasswordField` with native html:
```jsx
function App() {
const components = {
CurrentPasswordField: ({
fieldValidationErrors,
name,
onBlur,
onChange
}) => (
<>
{fieldValidationErrors?.map((error) => (
{error}
))}
>
)
};
}
```
Props:
The following example overrides the error message component with [`Heading`](/components/heading) and [`Text`](/components/text) primitives.
```jsx
import { Heading, Text } from '@aws-amplify/ui-react';
function App() {
const components = {
ErrorMessage: ({ children }) => (
<>
Custom Error Message{children}
>
)
};
return (
);
}
```
Props:
You can reuse the `AccountSettings.ChangePassword.SubmitButton`:
```jsx
import { AccountSettings } from '@aws-amplify/ui-react';
const components = {
SubmitButton: (props) => (
My Custom Button
)
}
```
or use [button](/components/button) primitive directly:
```jsx
import { AccountSettings, Button } from '@aws-amplify/ui-react';
const components = {
SubmitButton: (props) =>
}
```
The following example replaces `SubmitButton` with native html button:
```jsx
function App() {
const components = {
SubmitButton: ({ onClick, isDisabled }) => (
)
};
return (
);
}
```
### Theming
`ChangePassword` component is built upon our robust [Amplify UI theming system](../../theming). To get started, add a theme object and set the appropriate [design tokens](../../theming#design-tokens). You can then pass that `theme` to the `ThemeProvider` so the changes can take affect.
```jsx
import { AccountSettings, Card, ThemeProvider } from '@aws-amplify/ui-react';
const theme = {
name: 'custom-theme',
tokens: {
colors: {
border: {
primary: { value: '{colors.neutral.40}' },
secondary: { value: '{colors.neutral.20}' },
tertiary: { value: '{colors.neutral.10}' },
},
},
radii: {
small: { value: '2px' },
medium: { value: '3px' },
large: { value: '4px' },
xl: { value: '6px' },
},
},
};
function App() {
return (
);
}
```